package com.example.boos.updateStoreAddr;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.example.boos.HttpUtil.MyPost;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.map.ZdpMapView;
import com.example.boos.pojo.Boss;
import com.example.boos.pojo.StoreAddress;
import com.example.boos.util.MyThreadPool;

public class UpdateStoreAddr extends AppCompatActivity {
    //==================================================
    // 普通控件
    private TextView storeName; // 店铺名称
    private TextView addrAddress; // 显示店铺地址
    private Button addrUpdateAddr; // 修改店铺地址按钮
    private EditText addrBottom; // 店铺详细地址
    private Button saveAddress; // 保存地址按钮
    //==================================================
    /*百度地图控件*/
    private MapView mMapView;

    //================================================
    /*数据*/
    private StoreAddress storeAddress;

    //===================================================
    /*handler*/
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    Boolean ack = (Boolean) msg.obj;
                    if(ack){
                        // 地址更新成功
                        Toast.makeText(UpdateStoreAddr.this,"地址更新成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }else{
                        // 地址更新失败
                        Toast.makeText(UpdateStoreAddr.this,"地址更新失败，请重试",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_store_addr);
        initView();
        initData();
        initListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.back_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    private void initView(){
        storeName = findViewById(R.id.updateAddr_storeName);
        addrAddress  =  findViewById(R.id.addrAddress);
        addrUpdateAddr =  findViewById(R.id.addrUpdateAddr);
        addrBottom =  findViewById(R.id.addrBottom);
        saveAddress =  findViewById(R.id.saveAddress);
        mMapView =  findViewById(R.id.addrBaiduMap);
    }

    private void initData(){
        // 初始化StoreAddress
        storeAddress = new StoreAddress();
        storeAddress.setId(BossInfo.currentStoreAddress.getId());
        storeAddress.setStoreId(BossInfo.currentStoreAddress.getStoreId());
        storeAddress.setBottome(BossInfo.currentStoreAddress.getBottome());
        storeAddress.setAddress(BossInfo.currentStoreAddress.getAddress());
        storeAddress.setLatitude(BossInfo.currentStoreAddress.getLatitude());
        storeAddress.setLongitude(BossInfo.currentStoreAddress.getLongitude());

        storeName.setText(BossInfo.currentStore.getStoreName());
        // 设置店铺地址
        addrAddress.setText(BossInfo.currentStoreAddress.getAddress());
        // 详细地址
        addrBottom.setText(BossInfo.currentStoreAddress.getBottome());
    }

    private void initListener(){
        // 设置监听器
        // 修改地址
        addrUpdateAddr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("fromActivity","UpdateStoreAddr");
                Bundle bundle = new Bundle();
                bundle.putSerializable("storeAddress",storeAddress);
                intent.putExtras(bundle);
                intent.setComponent(new ComponentName(UpdateStoreAddr.this, ZdpMapView.class));
                UpdateStoreAddr.this.startActivityForResult(intent,99);
            }
        });

        // 保存地址
        saveAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String bottome = addrBottom.getText().toString();
                if(!"".equals(bottome)){
                    storeAddress.setBottome(bottome);
                }else{
                    Toast.makeText(UpdateStoreAddr.this,"请输入详细地址",Toast.LENGTH_SHORT).show();
                    return;
                }
                MyThreadPool.doJob(new Runnable() {
                    @Override
                    public void run() {
                        Boolean ack = (Boolean) MyPost.doPost("updateStoreAddr",storeAddress);
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = ack;
                        if(ack){
                            // 更新成功，更新一下 BossInfo里面的数据
                            BossInfo.currentStoreAddress =  storeAddress;
                            BossInfo.cur = new LatLng(Double.parseDouble(BossInfo.currentStoreAddress.getLatitude()),
                                    Double.parseDouble(BossInfo.currentStoreAddress.getLongitude()));
                        }
                        handler.sendMessage(msg);
                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 99){
            if(resultCode == 1){
                Bundle bundle = data.getExtras();
                StoreAddress result = (StoreAddress) bundle.getSerializable("StoreAddress");
                storeAddress.setLatitude(result.getLatitude());
                storeAddress.setLongitude(result.getLongitude());
                storeAddress.setAddress(result.getAddress());
                // 更新UI
                addrAddress.setText(storeAddress.getAddress());
            }
        }
    }
}